package org.jeecg.modules.friends.dao.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.friends.condition.ReadyToDeleteCondition;
import org.jeecg.modules.friends.mapper.ext.ReadyToDeleteDOExtMapper;
import org.jeecg.modules.friends.model.ReadyToDeleteDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReadyToDeleteDAOImpl implements org.jeecg.modules.friends.dao.ReadyToDeleteDAO {
    @Autowired
    private ReadyToDeleteDOExtMapper readyToDeleteDOExtMapper;

    @Override
    public void insert(ReadyToDeleteDO deleteDO) {
        readyToDeleteDOExtMapper.insert(deleteDO);
    }

    @Override
    public ReadyToDeleteDO findByUserId(Long userId) {
        ReadyToDeleteCondition readyToDeleteCondition = new ReadyToDeleteCondition();
        ReadyToDeleteCondition.Criteria criteria = readyToDeleteCondition.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<ReadyToDeleteDO> readyToDeleteDOList = readyToDeleteDOExtMapper.selectByCondition(readyToDeleteCondition);
        return CollectionUtils.isNotEmpty(readyToDeleteDOList) ? readyToDeleteDOList.get(0) : null;
    }

    @Override
    public void deleteByUserId(Long userId) {
        ReadyToDeleteCondition readyToDeleteCondition = new ReadyToDeleteCondition();
        ReadyToDeleteCondition.Criteria criteria = readyToDeleteCondition.createCriteria();
        criteria.andUserIdEqualTo(userId);
        readyToDeleteDOExtMapper.deleteByCondition(readyToDeleteCondition);
    }

    @Override
    public List<ReadyToDeleteDO> queryPastTimeRecords() {
        ReadyToDeleteCondition readyToDeleteCondition = new ReadyToDeleteCondition();
        ReadyToDeleteCondition.Criteria criteria = readyToDeleteCondition.createCriteria();
        criteria.andExpireTimeLessThan(System.currentTimeMillis());
        criteria.andStatusEqualTo(1);
        return readyToDeleteDOExtMapper.selectByCondition(readyToDeleteCondition);
    }

    @Override
    public void updateFinishedBatch(Set<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            readyToDeleteDOExtMapper.updateFinishedBatch(ids);
        }
    }
}